import api from '../../api/http'
import router ,{routerz}  from '../../router';
import { formatData,generateRouter } from '../../util/toTree';
export default {
    namespaced:true,
    state:{
        permissionlist:null,
        menuList:''
    },
    mutations:{
        SET_PERMISSION(state,routes){
            state.permissionlist = routes;
        },
        CLEAR_PERMISSION(state){
            state.permissionlist =null;
        },
        SET_MENU(state,menu){
            state.menuList = menu;
        },
        CLEAR_MENU(state){
            state.menuList = null;
        }
    },
    getters:{

    },
    actions:{
       async FETCH_PERMISSION({commit}){
      try{
        let menuData = await  api.getmenu()
        // console.log(menuData.data.authData)
        let menulist = formatData(menuData.data.authData)
        // 设置成路由结构了
        let dynamicRouter= generateRouter(menulist)
        
        // console.log(router.options.routes)
        const mainContainer =routerz.find(v => {return v.name ==='Layout'})
        const children = mainContainer.children;
        //数据改造一下
        const _children = JSON.parse(JSON.stringify(children))
        _children.push(...menulist)
        commit('SET_MENU',_children)

        children.push(...dynamicRouter)
        const initialRoutes  = router.options.routes;
        router.addRoutes(routerz)
        commit('SET_PERMISSION',[...initialRoutes,...routerz])
      
    
        
        
      } catch(err){
        throw err
      }
          
        }
    }
    
    
}